﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HA.ModFlow.Domain.DomainLayer;
namespace HA.Data
{
    [Serializable]
    public class BCSpecifiedFlow:BCType,ICloneable
    {


        public BCSpecifiedFlow()
            : base()
        {
            this._isLnType = true;

            this._isPyType = true;

            //this._multiSpecifiedFlow = new List<double>();

  
            //for (int i = 0; i < this.NPeriods; i++)
            //{
            //    this._multiSpecifiedFlow.Add(0);
            //}

 
        }


        //public override void OnNPeriodsChanged()
        //{
        //    base.OnNPeriodsChanged();

        //    //取得之前数据的个数
        //    int n = this.MultiSpecifiedFlow.Count;



        //    //开始更新数据


        //    if (n < this.NPeriods)
        //    {
        //        int nSub = this.NPeriods - n;

        //        for (int i = 0; i < nSub; i++)
        //        {
        //            this.MultiSpecifiedFlow.Add(0);
        //        }
        //    }

        //    else
        //    {
        //        int nSub = n - this.NPeriods;
        //        this.MultiSpecifiedFlow.RemoveRange(this.NPeriods, nSub);
        //    }


        //    //this.NPeriods;

        //}


        public object Clone()
        {
            BCSpecifiedFlow bcspf = (BCSpecifiedFlow)this.MemberwiseClone();

            bcspf._multiSpecifiedFlow = (BCDateDataCollection)this._multiSpecifiedFlow.Clone();

            return bcspf;
        }




        BCDateDataCollection _multiSpecifiedFlow = new BCDateDataCollection();

        public BCDateDataCollection MultiSpecifiedFlow
        {
            get { return _multiSpecifiedFlow; }
            set { _multiSpecifiedFlow = value; }
        }




        //double _specifiedFlow;

        public double SpecifiedFlow
        {
            get { return this._multiSpecifiedFlow.IsDataConst ? _multiSpecifiedFlow.ConstData : this._multiSpecifiedFlow.BCDatas[0].Value; }
            set
            {
                if (this._multiSpecifiedFlow.IsDataConst)
                    this._multiSpecifiedFlow.ConstData = value;
                else
                    this._multiSpecifiedFlow.BCDatas[0].Value = value;
            }
        }



        public override void MapToModflow(List<double> stressPeriods, List<ValueResult> ijkList, ModFlow.Domain.DomainLayer.ModFlowData mf, FeatureObject owner)
        {
          


            //对应到井包



            List<int> indice = new List<int>();

            List<Well> wells = new List<Well>();

            double TotalValue = owner.GetTotalLValue();

            int layerCount = 0;
            List<int> ks = new List<int>();
            for (int i = 0; i < ijkList.Count; i++)
            {
                if (!ks.Contains(ijkList[i].K))
                {
                    ks.Add(ijkList[i].K);
                    layerCount++;
                }
            }

            foreach (ValueResult item in ijkList)
            {
                indice.Add(mf.GridData.GetIndex(item.I, item.J, item.K));

                Well well = new Well(item.I, item.J, item.K, mf.GridData.RowsCount, mf.GridData.ColsCount, mf.GridData.LayersCount, mf.StressPeriods.InnerList.Count);

                this.assistMapToModFlow(stressPeriods, well.RechargeRates, this._multiSpecifiedFlow);

                for (int i = 0; i < stressPeriods.Count; i++)
                {
                    well.RechargeRates[i] *= (item.Value / TotalValue) / layerCount;
                }

                //for (int i = 0; i < this.NPeriods; i++)
                //{
                //    well.RechargeRates[i] = this._multiSpecifiedFlow[i] * (item.Value / TotalValue)/layerCount;
                //    //well.RechargeRates[i] = this._multiSpecifiedFlow[i] * item.Value;
                //}
                wells.Add(well);
            }

            //mf.SourceSinks.Wells.UpdateWells(wells, indice);
            mf.SourceSinks.Wells.AddTempWells(wells);
            
        }
    }
}
